package com.flower.service.impl;

import com.flower.mapper.ManagerMapper;
import com.flower.model.dto.ManagePageQueryDto;
import com.flower.model.dto.UserDto;
import com.flower.model.dto.UserPasswordDTO;
import com.flower.model.entity.Admin;
import com.flower.result.PageResult;
import com.flower.service.ManagerService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.flower.model.entity.Manager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@SuppressWarnings("all")
/**
 *@ClassName ManagerServiceImpl
 *@Description TODO
 *@Author @O_o  GW__
 *@Date 2025/6/12 16:14
 *@Version 1.0
 */
@Service
public class ManagerServiceImpl implements ManagerService {
    @Autowired
    private ManagerMapper managerMapper;

    @Override
    public PageResult pageQuery(ManagePageQueryDto managePageQueryDto) {
        PageHelper.startPage(managePageQueryDto.getPageNum(), managePageQueryDto.getPageSize());
        Page<Admin> page = managerMapper.pageQuery(managePageQueryDto);

        System.out.println(page.getTotal());
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void updateManager(UserDto userDto) {
        managerMapper.updateAdmin(userDto);
    }

    @Override
    public void updateEnable(UserDto userDto) {
        managerMapper.updateEnableById(userDto);
    }

    @Override
    public void deleteManager(Integer adid) {
        managerMapper.deleteManagerById(adid);
    }

    @Override
    public void deleteManagerList(List<Integer> ids) {
        ids.forEach(id -> {
            managerMapper.deleteManagerById(id);
        });
    }

    @Override
    public PageResult getAllManager(ManagePageQueryDto managePageQueryDto) {
        PageHelper.startPage(managePageQueryDto.getPageNum(), managePageQueryDto.getPageSize());
        Page<Admin> page = managerMapper.pageQuery(managePageQueryDto);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public Manager getByAdname(String adname) {
        return managerMapper.selectByAdname(adname);
    }

    @Override
    public void Update(Manager manager) {

        managerMapper.update(manager);
    }

    @Override
    public void UpdatePwd(UserPasswordDTO userPasswordDTO) {
        Manager mangers = managerMapper.getManagerById(userPasswordDTO.getAdname());
        if (!userPasswordDTO.getAdpwd().equals(mangers.getAdpwd())) {
            throw new RuntimeException("密码错误");
        } else {
            managerMapper.updatePwd(userPasswordDTO);
        }
    }
}
